import request from './request.js'

// 获取所有分类
export function getAllCategories() {
  return request({
    url: '/Categories/selectAll',
    method: 'get'
  })
}

// 根据ID获取分类
export function getCategoryById(id) {
  return request({
    url: `/Categories/${id}`,
    method: 'get'
  })
}

// 根据父分类获取子分类
export function getCategoriesByParentId(parentId) {
  return request({
    url: `/Categories/parent/${parentId}`,
    method: 'get'
  })
}

// 添加分类
export function addCategory(data) {
  return request({
    url: '/Categories/add',
    method: 'post',
    data
  })
}

// 更新分类
export function updateCategory(id, data) {
  return request({
    url: `/Categories/update/${id}`,
    method: 'put',
    data
  })
}

// 删除分类
export function deleteCategory(id) {
  return request({
    url: `/Categories/${id}`,
    method: 'delete'
  })
}

// 卖家添加分类
export function addSellerCategory(data) {
  return request({
    url: '/Categories/seller/add',
    method: 'post',
    data
  })
}

const getCategoryNameById = (categoryId) => {
  const category = categories.value.find(cat => cat.id === categoryId)
  return category ? category.name : '未分类'
}